package com.andorid.settings.fuelgauge;

import android.app.ActionBar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class PowerSavingActivity extends Activity implements OnClickListener {

	private static final int[] BATTERY_IMAGE_RESOURCE_ID = { R.drawable.battery_0,
			R.drawable.battery_1, R.drawable.battery_2, R.drawable.battery_3, R.drawable.battery_4,
			R.drawable.battery_5, R.drawable.battery_6, R.drawable.battery_7, R.drawable.battery_8,
			R.drawable.battery_9, R.drawable.battery_10, R.drawable.battery_11,
			R.drawable.battery_12, R.drawable.battery_13, R.drawable.battery_14,
			R.drawable.battery_15, R.drawable.battery_16, R.drawable.battery_17,
			R.drawable.battery_18, R.drawable.battery_19, R.drawable.battery_20 };

	private TextView mBatteryTextView;
	private ImageView mBtteryImage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_power_saving);

		this.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		this.getActionBar().setCustomView(R.layout.custom_actionbar);
		this.getActionBar().getCustomView().findViewById(R.id.switch1).setVisibility(View.GONE);
		;
		this.getActionBar().getCustomView().findViewById(R.id.back).setOnClickListener(this);
		super.findViewById(R.id.power_mode).setOnClickListener(this);
		super.findViewById(R.id.power_low_mode).setOnClickListener(this);

		mBatteryTextView = (TextView) super.findViewById(R.id.battery_textview);
		mBtteryImage = (ImageView) super.findViewById(R.id.battery_image);
	}

	public void onResume() {
		super.onResume();
		registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
	}

	public void onPause() {
		super.onPause();
		unregisterReceiver(mBatteryInfoReceiver);
	}

	private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
				int level = intent.getIntExtra("level", 0);
				mBatteryTextView.setText(level + "%");
				mBtteryImage.setImageResource(getBatteryImage(level));
			}
		}
	};

	private int getBatteryImage(int level) {
		int battery = level / 5;
		return BATTERY_IMAGE_RESOURCE_ID[battery];
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		switch (v.getId()) {
		case R.id.power_mode:
			intent.setClass(this, PowerModeActivity.class);
			startActivity(intent);
			break;
		case R.id.power_low_mode:
			intent.setClass(this, PowerLowModeActivity.class);
			startActivity(intent);
			break;
		case R.id.back:
			finish();
			break;
		}

	}
}
